package org.example.aaaatest.a1;

import com.amap.car.order.i18n.util.TimeZoneUtil;

import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @description:
 * @author: wangbaogui
 * @create: 2025/4/9 21:25
 **/
public class B1 {
    public static void main(String[] args) {

        int i = calculateHoursDifference();
        System.out.println("i = " + i);


    }

    public static int calculateHoursDifference() {
        // 定义东八区的ZoneId（如上海）
        TimeZone timeZone = TimeZoneUtil.getAdminTimeZone(39.762315,35.713246);
        ZoneId zoneId = timeZone.toZoneId();
        ZoneId eastEightZoneId = ZoneId.of("Asia/Shanghai");

        // 获取当前时间的Instant


        // 将Instant转换为两个时区的ZonedDateTime
        Instant now = Instant.now();
        ZoneOffset offsetEastEight = zoneId.getRules().getOffset(now);
        ZoneOffset offsetTarget = eastEightZoneId.getRules().getOffset(now);

        Long differenceSeconds = 0L;
        // 计算时间差（单位：小时）
        if(offsetTarget.getTotalSeconds() > offsetEastEight.getTotalSeconds()){
            differenceSeconds  = (long) (offsetTarget.getTotalSeconds() - offsetEastEight.getTotalSeconds());
        }else {
             differenceSeconds = (long) (offsetEastEight.getTotalSeconds() - offsetTarget.getTotalSeconds());
        }

        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MILLISECOND, (int)(differenceSeconds * 1000));

        // 获取修改后的Date对象
        Date newDate = calendar.getTime();


        System.out.println("differenceHours = " + newDate);
        return 1;
    }
}
